package com.ng.biz.controller;

import java.util.Date;
import java.util.List;

import org.springframework.web.bind.annotation.*;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.ng.biz.model.domain.ParamModel;
import com.ng.biz.model.domain.ParamModelCategory;
import com.ng.common.annotation.TokenRequired;
import com.ng.common.annotation.Tx;
import com.ng.common.controller.Controller;
import com.ng.common.model.response.R;
import com.ng.common.util.UuidUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;

@Tag(name = "参数模型管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("/biz/param/model")
public class ParamModelController extends Controller {
	
	@Operation(summary = "获取参数模型类别列表")
	@Parameters({
		@Parameter(name = "keywords", description = "关键字", required = false),
		@Parameter(name = "cur", description = "当前页码", required = true),
		@Parameter(name = "limit", description = "每页数据量", required = true)
	})
	@TokenRequired
	@GetMapping("/category/pages")
	public R<List<ParamModelCategory>> pagesCategory(
			HttpServletRequest request,
			@RequestParam(name = "keywords", required = false, defaultValue = "") String keywords,
			@RequestParam(name = "cur", required = false, defaultValue = "1") int cur,
			@RequestParam(name = "limit", required = false, defaultValue = "10") int limit) {
		
		String sqlExceptSelect = " from " + ParamModelCategory.TABLE_NAME + " a WHERE 1=1 ";
		if (!StrKit.isBlank(keywords)) {
			sqlExceptSelect += " and a.name like '%" + keywords + "%' ";
		}
		sqlExceptSelect += " ORDER BY a.sort asc ";
		List<ParamModelCategory> page = ParamModelCategory.dao.find("select a.* " + sqlExceptSelect);
		return R.ok(page);
	}

	@Operation(summary = "保存参数模型类别信息")
	@Parameters({
		@Parameter(name = "paramModelCategory", description = "参数模型类别信息", required = true)
	})
	@TokenRequired
	@Tx
	@PostMapping("/category/save")
	public R<ParamModelCategory> saveCategory(HttpServletRequest request, @RequestBody ParamModelCategory paramModelCategory) {
		try {
			if (StrKit.notBlank(paramModelCategory.getId())) {
				paramModelCategory.setUpdateUserId(getUserId(request));
				paramModelCategory.setUpdateTime(new Date(System.currentTimeMillis()));
				if (paramModelCategory.update()) {
					return R.ok(paramModelCategory);
				} else {
					return R.failed("更新失败！");
				}
			} else {
				paramModelCategory.setId(UuidUtil.getUUID());
				paramModelCategory.setCreateUserId(getUserId(request));
				paramModelCategory.setCreateTime(new Date(System.currentTimeMillis()));
				if (paramModelCategory.save()) {
					return R.ok(paramModelCategory);
				} else {
					return R.failed("保存失败.");
				}
			}
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@Operation(summary = "删除参数模型类别信息")
	@Parameters({
		@Parameter(name = "ids", description = "参数模型类别id集合(逗号分割)", required = true)
	})
	@DeleteMapping("/category/{ids}")
	@TokenRequired
	@Tx
	public R<String> deleteCategory(@PathVariable("ids") String ids) {
		String[] arr = ids.split(",");
		if (ids == null || ids.length() <= 0) {
			return R.failed("请选择需要删除的参数模型类别信息.");
		}
		for (String id : arr) {
			ParamModelCategory.dao.deleteById(id);
			Db.delete("delete from "+ParamModel.TABLE_NAME+" where category_id=?",id);
		}
		return R.ok();
	}

	@Operation(summary = "获取参数模型列表")
	@Parameters({
		@Parameter(name = "keywords", description = "关键字", required = false),
		@Parameter(name = "categoryId", description = "类别ID", required = false),
		@Parameter(name = "cur", description = "当前页码", required = true),
		@Parameter(name = "limit", description = "每页数据量", required = true)
	})
	@TokenRequired
	@GetMapping("/pages")
	public R<Page<ParamModel>> pages(
			HttpServletRequest request,
			@RequestParam(name = "keywords", required = false, defaultValue = "") String keywords,
			@RequestParam(name = "categoryId", required = false, defaultValue = "") String categoryId,
			@RequestParam(name = "cur", required = false, defaultValue = "1") int cur,
			@RequestParam(name = "limit", required = false, defaultValue = "10") int limit) {
		
		String sqlExceptSelect = " from " + ParamModel.TABLE_NAME + " a WHERE 1=1 ";
		if (!StrKit.isBlank(keywords)) {
			sqlExceptSelect += " and a.name like '%" + keywords + "%' ";
		}
		if (!StrKit.isBlank(categoryId)) {
			sqlExceptSelect += " and a.category_id = '" + categoryId + "' ";
		}
		sqlExceptSelect += " ORDER BY a.sort asc ";
		Page<ParamModel> page = ParamModel.dao.paginate(cur, limit, "select a.* ", sqlExceptSelect);
		return R.ok(page);
	}

	@Operation(summary = "保存参数模型信息")
	@Parameters({
		@Parameter(name = "paramModel", description = "参数模型信息", required = true)
	})
	@TokenRequired
	@Tx
	@PostMapping("/save")
	public R<ParamModel> save(HttpServletRequest request, @RequestBody ParamModel paramModel) {
		try {
			if (StrKit.notBlank(paramModel.getId())) {
				paramModel.setUpdateUserId(getUserId(request));
				paramModel.setUpdateTime(new Date(System.currentTimeMillis()));
				if (paramModel.update()) {
					return R.ok(paramModel);
				} else {
					return R.failed("更新失败！");
				}
			} else {
				paramModel.setId(UuidUtil.getUUID());
				paramModel.setCreateUserId(getUserId(request));
				paramModel.setCreateTime(new Date(System.currentTimeMillis()));
				if (paramModel.save()) {
					return R.ok(paramModel);
				} else {
					return R.failed("保存失败.");
				}
			}
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@Operation(summary = "删除参数模型信息")
	@Parameters({
		@Parameter(name = "ids", description = "参数模型id集合(逗号分割)", required = true)
	})
	@DeleteMapping("/{ids}")
	@TokenRequired
	@Tx
	public R<String> delete(@PathVariable("ids") String ids) {
		String[] arr = ids.split(",");
		if (ids == null || ids.length() <= 0) {
			return R.failed("请选择需要删除的参数模型信息.");
		}
		for (String id : arr) {
			ParamModel.dao.deleteById(id);
		}
		return R.ok();
	}

}
